package io.netty.shenh.example.inboundoutbound;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * 在ByteToMessageDecoder中会在while循环中判断in是否有数据可读, 如果有, 会调用子类的decode()方法,
 * 读取一次后正常传播下去, 传播完后会进入下一个循环, 回到ByteToMessageDecoder
 */
public class MyByte2LongDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {
//        System.out.println("MyByte2LongDecoder-decode");
        if (in.readableBytes() >= 8) {
            out.add(in.readLong());
        }
    }
}
